package cn.sc.allinpay.allfavour.business.ewallet;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import java.math.BigDecimal;

import cn.pai.common.utils.AmtUtils;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.data.MemberData;
import cn.sc.allinpay.allfavour.business.common.data.OrderData;

/**
 * 电子钱包 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class EWalletPresenter extends PaiPresenter<EWalletContract.View> implements EWalletContract.Presenter {

    private EWalletModel model = new EWalletModel();

    private OrderData payOrder;//支付订单
    private BigDecimal transAmt;//交易金额
    private MemberData member;//会员信息
    private BigDecimal balAmt;//剩余金额 减去会员权益折扣和卡券折扣后的金额

    private BigDecimal discountAmt;//电子钱包消费金额
    private BigDecimal paidAmt;//最终剩余金额

    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Bundle bundle = view.getBundle();
                payOrder = bundle.getParcelable(Constant.BundleKey.PAY_ORDER);
            }
        };
    }

    @Override
    public void start() {
        if (initData()) {
            initView();
        } else {
            view.finish();
        }
    }

    /**
     * 初始化数据
     *
     * @return
     */
    private boolean initData() {
        if (payOrder == null) {
            view.toast("订单数据不能为空");
            return false;
        }
        if (TextUtils.isEmpty(payOrder.getTransAmt())) {
            view.toast("交易金额不能为空");
            return false;
        }
        if (payOrder.getMemberData() == null) {
            view.toast("会员信息不能为空");
            return false;
        }
        transAmt = new BigDecimal(payOrder.getTransAmt());//交易金额
        member = payOrder.getMemberData();//会员数据

        balAmt = new BigDecimal(payOrder.getTransAmt());//剩余金额 减去会员权益折扣和卡券折扣后的金额
        if (!TextUtils.isEmpty(payOrder.getMemberData().getOilDiscountAmt().toString())) {//会员权益优惠
            balAmt = balAmt.subtract(new BigDecimal(payOrder.getMemberData().getOilDiscountAmt().toString()));
        }
        if (!TextUtils.isEmpty(payOrder.getDisCouponAmt())) {//会员卡券优惠
            balAmt = balAmt.subtract(new BigDecimal(payOrder.getDisCouponAmt()));
        }
        return true;
    }

    /**
     * 初始化视图
     */
    private void initView() {
        view.showTransAmt(AmtUtils.formatY(transAmt));//交易金额

        view.showMemberLevel(member.getLevel());
        view.showMemberName(member.getMemberName());
        view.showMemberNo(member.getCardNo());

        String eWalletBal = AmtUtils.changeF2Y(member.getBal());//电子钱包余额
        view.showEWalletBal(eWalletBal);//电子钱包余额
        if (balAmt.compareTo(new BigDecimal(eWalletBal)) > 0) {
            view.setEWalletMaxAmt(eWalletBal);//设置最大输入金额，电子钱包余额
        } else {
            view.setEWalletMaxAmt(balAmt.toString());//设置最大输入金额，减去会员权益折扣和卡券折扣后的金额
        }

        if (!TextUtils.isEmpty(payOrder.getDisEWalletAmt())) {//电子钱包消费金额不为空
            view.showEWalletAmt(AmtUtils.formatY(payOrder.getDisEWalletAmt()));//电子钱包消费金额，会自动触发金额计算
        } else {
            calculateAmt();
        }
    }

    /**
     * 计算金额
     */
    @Override
    public void calculateAmt() {
        String eWalletAmtStr = view.getEWalletAmt();//电子钱包消费金额
        discountAmt = (!TextUtils.isEmpty(eWalletAmtStr) ? new BigDecimal(eWalletAmtStr) : BigDecimal.ZERO);
        paidAmt = balAmt.subtract(discountAmt);//剩余金额=消费金额-电子钱包消费金额
        if (BigDecimal.ZERO.compareTo(paidAmt) > 0) {//剩余金额为负数，直接至为0
            paidAmt = BigDecimal.ZERO;
        }
        view.showPaidAmt(AmtUtils.formatY(paidAmt));
    }

    @Override
    public void setActivityForResult() {
        if (discountAmt == null) {
            view.prompt("消费金额不能为空");
            return;
        }
        if (paidAmt == null) {
            view.prompt("剩余金额不能为空");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString(Constant.BundleKey.DISCOUNT_AMT, (BigDecimal.ZERO.compareTo(discountAmt) != 0 ? discountAmt.toString() : null));//优惠金额
        bundle.putString(Constant.BundleKey.PAID_AMT, paidAmt.toString());//剩余金额
        view.setResult(Activity.RESULT_OK, bundle);
        view.finish();
    }
}
